package com.cloud.hotline.common.utils;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.cloud.hotline.api.entity.ToHotlineRevisitRecord;
import com.cloud.hotline.api.entity.ToHotlineStreetRecord;
import com.cloud.hotline.api.entity.ToHotlineUnitRecord;

public class ValidationHelper {
	// 校验通过
	private static final String SUCCESS = "success";
	// 校验不通过
	private static final String FAIL = "fail";

	/**
	 * 校验市级别回访数据
	 * 
	 * @param records
	 *            模板中的数据
	 * @param end
	 * @param start
	 * @return
	 */
	public static Map<String, List<ToHotlineRevisitRecord>> validataCityRevisitRecord(
			List<ToHotlineRevisitRecord> records, String start, String end, List<String> unitNames) {
		List<ToHotlineRevisitRecord> success = new ArrayList<ToHotlineRevisitRecord>();
		List<ToHotlineRevisitRecord> fail = new ArrayList<ToHotlineRevisitRecord>();
		for (ToHotlineRevisitRecord record : records) {
			if (StringUtils.isEmpty(record.getYesswNum())) {
				record.setErrorMsg(record.getErrorMsg() + "市长热线编号未填写！");
			}
			if (null == record.getDispatchTime()) {
				record.setErrorMsg(record.getErrorMsg() + "派单时间未填写！");
			} else {
				Date startDate = DateHelper.strToFormatDate(start, "yyyy-MM-dd HH:mm");
				Date endDate = DateHelper.strToFormatDate(end, "yyyy-MM-dd HH:mm");
				if (record.getDispatchTime().before(startDate) || record.getDispatchTime().after(endDate)) {
					record.setErrorMsg(record.getErrorMsg() + "派单时间不在选定的时间周期内！");
				}
			}
			if (StringUtils.isEmpty(record.getFinishFlag())) {
				record.setErrorMsg(record.getErrorMsg() + "问题解决不能为空！");
			}
			if (StringUtils.isEmpty(record.getPleasedFlag())) {
				record.setErrorMsg(record.getErrorMsg() + "问题处理满意不能为空！");
			}
			if (!unitNames.contains(record.getUnitName())) {
				record.setErrorMsg(record.getErrorMsg() + "未找到对应的处置部门！");
			}
			if (StringUtils.isEmpty(record.getErrorMsg())) {
				success.add(record);
			} else {
				fail.add(record);
			}
		}
		Map<String, List<ToHotlineRevisitRecord>> result = new HashMap<String, List<ToHotlineRevisitRecord>>();
		result.put(SUCCESS, success);
		result.put(FAIL, fail);
		return result;
	}

	/**
	 * 校验区级回访数据
	 * 
	 * @param unitNams 
	 * @param end 开始时间
	 * @param start 截止时间
	 * @param records 模板中的数据
	 * 
	 * @return
	 */
	public static Map<String, List<ToHotlineUnitRecord>> validateAreaRevisitRecord(List<ToHotlineUnitRecord> records,
			String start, String end, List<String> unitNames) {
		List<ToHotlineUnitRecord> success = new ArrayList<ToHotlineUnitRecord>();
		List<ToHotlineUnitRecord> fail = new ArrayList<ToHotlineUnitRecord>();
		for (ToHotlineUnitRecord record : records) {
			if (StringUtils.isEmpty(record.getYesswNum())) {
				record.setErrorMsg(record.getErrorMsg() + "市长热线编号未填写！");
			}
			if(!"是".equals(record.getReturnVisitFlag())){
				record.setErrorMsg(record.getErrorMsg() + "回访组未联系公众！");
			}else{
				if(StringUtils.isEmpty(record.getSloveFlag())  || StringUtils.isEmpty(record.getPleasedFlag())){
					record.setErrorMsg(record.getErrorMsg() + "已回访，但问题解决或满意度未填写！");
				}
			}
			if (null == record.getFeedbackTime()) {
				record.setErrorMsg(record.getErrorMsg() + "回访时间未填写！");
			} else {
				Date startDate = DateHelper.strToFormatDate(start, "yyyy-MM-dd HH:mm");
				Date endDate = DateHelper.strToFormatDate(end, "yyyy-MM-dd HH:mm");
				if (record.getFeedbackTime().before(startDate) || record.getFeedbackTime().after(endDate)) {
					record.setErrorMsg(record.getErrorMsg() + "回访时间不在选定的时间周期内！");
				}
			}
			if (!unitNames.contains(record.getUnitName())) {
				record.setErrorMsg(record.getErrorMsg() + "未找到对应的处置部门！");
			}
			if (StringUtils.isEmpty(record.getErrorMsg())) {
				success.add(record);
			} else {
				fail.add(record);
			}
		}
		Map<String, List<ToHotlineUnitRecord>> result = new HashMap<String, List<ToHotlineUnitRecord>>();
		result.put(SUCCESS, success);
		result.put(FAIL, fail);
		return result;
	}

	/**
	 * 校验街道承办
	 * 
	 * @param unitNams
	 *            机构名称
	 * @param records
	 *            Excel中的数据
	 * 
	 * @return
	 */
	public static Map<String, List<ToHotlineStreetRecord>> validateStreetUndertakeRecord(
			List<ToHotlineStreetRecord> records, List<String> unitNames) {
		List<ToHotlineStreetRecord> success = new ArrayList<ToHotlineStreetRecord>();
		List<ToHotlineStreetRecord> fail = new ArrayList<ToHotlineStreetRecord>();
		for (ToHotlineStreetRecord record : records) {
			if (StringUtils.isEmpty(record.getYesswNum())) {
				record.setErrorMsg(record.getErrorMsg() + "市长热线编号未填写！");
			}
			if (!unitNames.contains(record.getUnitName())) {
				record.setErrorMsg(record.getErrorMsg() + "未找到对应的处置部门！");
			}
			if (StringUtils.isEmpty(record.getErrorMsg())) {
				success.add(record);
			} else {
				fail.add(record);
			}
		}
		Map<String, List<ToHotlineStreetRecord>> result = new HashMap<String, List<ToHotlineStreetRecord>>();
		result.put(SUCCESS, success);
		result.put(FAIL, fail);
		return result;
	}
	
	/**
	 * 校验部门承办数据
	 * 
	 * @param unitNams 
	 * @param end 开始时间
	 * @param start 截止时间
	 * @param records 模板中的数据
	 * 
	 * @return
	 */
	public static Map<String, List<ToHotlineUnitRecord>> validateUnitUndertakeRecord(List<ToHotlineUnitRecord> records,
			String start, String end, List<String> unitNames) {
		List<ToHotlineUnitRecord> success = new ArrayList<ToHotlineUnitRecord>();
		List<ToHotlineUnitRecord> fail = new ArrayList<ToHotlineUnitRecord>();
		for (ToHotlineUnitRecord record : records) {
			if (StringUtils.isEmpty(record.getYesswNum())) {
				record.setErrorMsg(record.getErrorMsg() + "市长热线编号未填写！");
			}
			if(StringUtils.isEmpty(record.getUnitName())){
				record.setErrorMsg(record.getErrorMsg() + "处置部门未填写！");
			}else if (!unitNames.contains(record.getUnitName())) {
				record.setErrorMsg(record.getErrorMsg() + "未找到对应的处置部门！");
			}
			if (StringUtils.isEmpty(record.getErrorMsg())) {
				success.add(record);
			} else {
				fail.add(record);
			}
		}
		Map<String, List<ToHotlineUnitRecord>> result = new HashMap<String, List<ToHotlineUnitRecord>>();
		result.put(SUCCESS, success);
		result.put(FAIL, fail);
		return result;
	}
	

}
